﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class sort_manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        string name = Session["USERNAME"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_get = new SqlCommand();
        com_get.Connection = sc;
        com_get.CommandText = "select SORT_NAME,SORT_LEVEL from INFO_SORT where ENTER_ID=@eid1";
        com_get.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
        SqlDataReader reader = com_get.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        sc.Close();
    }
    protected void Delete(object sender, GridViewDeleteEventArgs e)
    {
      

        String sname = GridView1.Rows[e.RowIndex].Cells[0].Text;
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        SqlCommand del = new SqlCommand();
        del.Connection = sc1;
        del.CommandText = "delete from INFO_SORT where SORT_NAME=@name";
        del.Parameters.AddWithValue("@name", sname);
        while (Application["sqllock"] == "lock")
        {//检测是否已经锁住               
            System.Threading.Thread.Sleep(2000);
        }
        Application["sqllock"] = "lock";//上锁 
        if (del.ExecuteNonQuery()==1)
        { 
            Application["sqllock"] = "unlock";//解锁 
            SqlCommand del2 = new SqlCommand();
            del2.Connection = sc1;
            del2.CommandText = "delete from INFO_PRODUCT where PRODUCT_SORT=@namee";
            del2.Parameters.AddWithValue("@namee",sname);
            while (Application["sqllock"] == "lock")
            {//检测是否已经锁住               
                System.Threading.Thread.Sleep(2000);
            }
            Application["sqllock"] = "lock";//上锁 
            try
            {
                del2.ExecuteNonQuery();
            }
            finally
            {
                Application["sqllock"] = "unlock";//解锁 
            }
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('删除产品分类成功！')</script>");
            return;
        }
        else
        {
            Application["sqllock"] = "unlock";//解锁 
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
            return;
        }
    }
}